////////////////////////////  
function doClick(id) {
    var key;

    if (window.event)
        key = window.event.keyCode; // IE

    if (key == 13) {
        var btn = document.getElementById(id);

        if (btn != null) {
            btn.click();
        }
    }
}

// //////////////////////////// ajax //////////////////////////////////////////////////////////////////////
function Login() {

 
    if (!validateLogin())
        return false;

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("div_login").innerHTML = xmlhttp.responseText;
        }
    }

  
      
    xmlhttp.open("POST", "customers?action=login", true);
    xmlhttp.setRequestHeader("Content-type",
        "application/x-www-form-urlencoded");
    xmlhttp.send("username=" + document.getElementById("txtUserName").value
        + "&password=" + document.getElementById("txtpass").value);

    return false;
}

function Logout() {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            document.getElementById("div_login").innerHTML = xmlhttp.responseText;

    }

    xmlhttp.open("Get", "customers?action=logout", true);
    xmlhttp.send();

}

// //////////////////////////////// validation  //////////////////////////////////////////////////////////////

function validateLogin() {
    try {
        var username = document.getElementById("txtUserName").value;
        var password = document.getElementById("txtpass").value;

        if (username == '')
            throw 'Introduceti numele de utilizator';
        else if (password == '')
            throw 'Introduceti parola';
		
        return true;

    } catch (err) {

        var label = document.getElementById("lb_login_error");
        if (label)
            label.innerHTML = '<strong>EROARE: </strong>' + err;
        else {
            label = document.createElement('lb_login_error');
            label.setAttribute('id', 'lb_login_error');
            label.setAttribute('for', 'txtUserName');
            label.setAttribute('generated', 'true');
            label.setAttribute('class', 'error');

            label.innerHTML = '<strong>EROARE: </strong>' + err;
            document.getElementById("div_error_login").appendChild(label);
        }
        return false;
    }
}
